import ProxyHandler from "../sql/ProxyHandler";
import {BrowserWindow} from "electron";
import {v4 as uuidv4} from 'uuid';
import md5 from "crypto-js/md5";
import SystemLoyaltyMapper from "../dao/SystemLoyaltyMapper";
import SystemLoyalty from "../entity/SystemLoyalty";
import SystemGlobalMapper from "../dao/SystemGlobalMapper";
import SystemGlobal from "../entity/SystemGlobal";
import SystemMember from "../entity/SystemMember";

export default class SystemGlobalService {
    private mapper: SystemGlobalMapper;
    private handler: ProxyHandler<SystemGlobal>;

    constructor() {
        this.mapper = new SystemGlobalMapper()
        this.handler = new ProxyHandler<SystemGlobal>();
    }

    async get(){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }

        const result = await this.mapper.selectByDefault();

        win.webContents.send('main-to-renderer', {
            name: 'global-get-success',
            event: 'event',
            data: result ? result : null
        })
    }

    async save(data){
        const win = BrowserWindow.getFocusedWindow();
        if(win == null){
            throw new Error("应用程序错误")
        }
        const entity = new SystemGlobal();
        entity.store = data.store;
        entity.hotline = data.hotline;
        entity.address = data.address;
        if(data.id){
            entity.id = data.id;
            const proxy = new Proxy(this.mapper.updateById, this.handler)
            proxy(entity)
        }else{
            entity.id = md5(uuidv4()).toString();
            const proxy = new Proxy(this.mapper.insert, this.handler)
            proxy(entity)
        }

        win.webContents.send('main-to-renderer', {
            name: 'global-operator-success',
            event: 'event',
            data: '操作成功'
        })
    }
}
